{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mesh generation for tutorial 07"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import gmsh\n",
    "from mpi4py import MPI\n",
    "from dolfinx import MeshTags\n",
    "from dolfinx.io import XDMFFile\n",
    "from dolfinx.mesh import locate_entities\n",
    "from multiphenicsx.mesh.utils import gmsh_to_fenicsx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert MPI.COMM_WORLD.size == 1, \"This mesh generation notebook is supposed to be run in serial\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Geometrical parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = 1\n",
    "lcar = 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create mesh\n",
    "\n",
    "Generate a simple mesh consisting in an hexagon discretized with six equilateral triangle cells."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gmsh.initialize()\n",
    "gmsh.model.add(\"mesh\")\n",
    "points = [\n",
    "    gmsh.model.geo.addPoint(np.cos(t / 3 * np.pi), np.sin(t / 3 * np.pi), 0.0, lcar) for t in range(6)]\n",
    "lines = [gmsh.model.geo.addLine(points[t], points[(t + 1) % 6]) for t in range(6)]\n",
    "line_loop = gmsh.model.geo.addCurveLoop(lines)\n",
    "domain = gmsh.model.geo.addPlaneSurface([line_loop])\n",
    "gmsh.model.geo.synchronize()\n",
    "gmsh.model.addPhysicalGroup(1, lines, 0)\n",
    "gmsh.model.addPhysicalGroup(2, [domain], 0)\n",
    "gmsh.model.mesh.generate(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh, subdomains, boundaries = gmsh_to_fenicsx(gmsh.model, gdim=2)\n",
    "gmsh.finalize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create cell restrictions\n",
    "\n",
    "Define mesh tags on cells, which are equal to one on all cells."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cell_entities_all = locate_entities(\n",
    "    mesh, mesh.topology.dim, lambda x: np.full((x.shape[1], ), True))\n",
    "cell_values_all = np.full(cell_entities_all.shape, 1, dtype=np.intc)\n",
    "cell_restriction_all = MeshTags(mesh, mesh.topology.dim, cell_entities_all, cell_values_all)\n",
    "cell_restriction_all.name = \"cell_restriction_all\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define mesh tags on cells, which are equal to one on one half of the cells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eps = np.finfo(float).eps\n",
    "cell_entities_subset = locate_entities(\n",
    "    mesh, mesh.topology.dim,\n",
    "    lambda x: np.logical_or(x[0] < eps, np.logical_and(x[1] < eps, x[0] < 0.5 + eps)))\n",
    "cell_values_subset = np.full(cell_entities_subset.shape, 1, dtype=np.intc)\n",
    "cell_restriction_subset = MeshTags(mesh, mesh.topology.dim, cell_entities_subset, cell_values_subset)\n",
    "cell_restriction_subset.name = \"cell_restriction_subset\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create facet restrictions\n",
    "\n",
    "Define mesh tags on facets, which are equal to one on all facets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "facet_entities_all = locate_entities(\n",
    "    mesh, mesh.topology.dim - 1, lambda x: np.full((x.shape[1], ), True))\n",
    "facet_values_all = np.full(facet_entities_all.shape, 1, dtype=np.intc)\n",
    "facet_restriction_all = MeshTags(mesh, mesh.topology.dim - 1, facet_entities_all, facet_values_all)\n",
    "facet_restriction_all.name = \"facet_restriction_all\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define mesh tags on facets, which are equal to one on two facets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "facet_entities_subset = locate_entities(\n",
    "    mesh, mesh.topology.dim - 1, lambda x: np.fabs(x[1] + np.sqrt(3) * x[0]) < 0.01)\n",
    "facet_values_subset = np.full(facet_entities_subset.shape, 1, dtype=np.intc)\n",
    "facet_restriction_subset = MeshTags(mesh, mesh.topology.dim - 1, facet_entities_subset, facet_values_subset)\n",
    "facet_restriction_subset.name = \"facet_restriction_subset\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with XDMFFile(MPI.COMM_WORLD, \"hexagon.xdmf\", \"w\") as output:\n",
    "    output.write_mesh(mesh)\n",
    "    output.write_meshtags(cell_restriction_all)\n",
    "    output.write_meshtags(cell_restriction_subset)\n",
    "    mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim)\n",
    "    output.write_meshtags(facet_restriction_all)\n",
    "    output.write_meshtags(facet_restriction_subset)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython"
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
